package com.lanchetech.entity;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

@ApiModel(value = "com.bonisenhome.entity.Admin")
@Data
@Builder
@Accessors(chain = true)
@JsonIgnoreProperties({"handler"})
@NoArgsConstructor
@AllArgsConstructor
public class Admin implements UserDetails, Serializable {
    /**
     * id
     */
    @ApiModelProperty(value = "id")
    private Long id;

    /**
     * 管理员角色id
     */
    @ApiModelProperty(value = "管理员角色id")
    private Long roleId;

    /**
     * 管理员账号
     */
    @ApiModelProperty(value = "管理员账号")
    private String username;

    /**
     * 密码
     */
    @ApiModelProperty(value = "密码")
    private String password;

    /**
     * 昵称
     */
    @ApiModelProperty(value = "昵称")
    private String nickname;

    /**
     * 头像
     */
    @ApiModelProperty(value = "头像")
    private String avatar;

    /**
     * 最近一次修改密码时间
     */
    @ApiModelProperty(value = "最近一次修改密码时间")
    private Date lastPasswordResetDate;

    /**
     * 管理员状态 0 未知 1 正常
     */
    @ApiModelProperty(value = "管理员状态 0 未知 1 正常")
    private Integer status;

    /**
     * 创建时间
     */
    @ApiModelProperty(value = "创建时间")
    private Date createdAt;

    /**
     * 更新时间
     */
    @ApiModelProperty(value = "更新时间")
    private Date updatedAt;

    /**
     * 删除 0 未删除 1 已删除
     */
    @ApiModelProperty(value = "删除 0 未删除 1 已删除")
    private Byte deleted;

    /**
     * 姓名
     */
    @ApiModelProperty(value = "姓名")
    private String realName;

    private static final long serialVersionUID = 1L;

    private List<AdminPermission> adminPermissions;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> auths = new ArrayList<>();
        List<AdminPermission> permissions = this.getAdminPermissions();
        if(permissions != null){
            for (AdminPermission permission : permissions) {
                auths.add(new SimpleGrantedAuthority(permission.getPermission()));
            }
        }
        return auths;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}